package cn.com.czpb.client.module.news.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.FragmentVPAdapter;
import cn.com.czpb.client.model.news.NewsChannelBean;
import cn.com.czpb.client.module.news.NewsBaseFragment;
import cn.com.czpb.client.module.news.fragment.NewsColumnsItemFragment;
import cn.com.czpb.client.module.news.fragment.NewsHtmlFragment;
import cn.com.czpb.client.module.news.fragment.NewsItemFragment;
import cn.com.czpb.client.module.news.fragment.NewsItemFragmentMain;
import cn.com.czpb.client.module.news.fragment.ZhengWuFragment;
import cn.com.czpb.client.module.subscribe.SubscribeFragment;

import java.util.ArrayList;
import java.util.List;

public class NewsFragmentPagerAdapter extends FragmentVPAdapter<NewsBaseFragment> {
    private int selectedPosition = 0;
    private View view;
    TextView tv;
    Context context;

    public NewsFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    public void setSelectedPosition(int mselectedPosition) {
        this.selectedPosition = mselectedPosition;
    }

    public void sortChannel(List<NewsChannelBean> saveTitleList) {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment entry : fragments) {
            ft.remove(entry);
        }
        ft.commit();
        ft = null;
        fm.executePendingTransactions();
        List<NewsBaseFragment> list = new ArrayList<NewsBaseFragment>();


        for (int i = 0; i < saveTitleList.size(); i++) {
            NewsChannelBean ncb = saveTitleList.get(i);

            //头条主页
            if ("2".equals(ncb.getType())){
                NewsHtmlFragment newsHtmlFragment = NewsHtmlFragment.newInstance(i,ncb);
                newsHtmlFragment.setTitle(ncb.getCnname());
                list.add(newsHtmlFragment);
            }
            else if ("3".equals(ncb.getType())) {
                NewsItemFragmentMain suFm = NewsItemFragmentMain.newInstance(i, ncb);
                suFm.setTitle(ncb.getCnname());
                list.add(suFm);

                //耳机频道的栏目
            } else if ("4".equals(ncb.getType())) {
                NewsColumnsItemFragment itemFragment = NewsColumnsItemFragment.newInstance(i,ncb);
                itemFragment.setTitle(ncb.getCnname());
                list.add(itemFragment);
                //政务页面
            }else if ("5".equals(ncb.getType())) {
                ZhengWuFragment zhengWuFragment = ZhengWuFragment.newInstance();
                zhengWuFragment.setTitle(ncb.getCnname());
                list.add(zhengWuFragment);

                //h5 页面
            }else if ("6".equals(ncb.getType())) {
                SubscribeFragment subscribeFragment = new SubscribeFragment();
                subscribeFragment.setTitle(ncb.getCnname());
                list.add(subscribeFragment);
                //h5 页面
            }

            else {//普通新闻栏目
                NewsItemFragment itemFragment = NewsItemFragment.newInstance(i, ncb);
                itemFragment.setTitle(ncb.getCnname());
                list.add(itemFragment);
            }
        }
        setFragments(list);
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public View getTabView(int position) {

        view = LayoutInflater.from(context).inflate(R.layout.tab_comment, null,false);
        tv = view.findViewById(R.id.comment_mine);
        NewsBaseFragment frag = fragments.get(position);
        String ti = frag.getTitle();
        tv.setText(ti);
        return view;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        NewsBaseFragment frag = fragments.get(position);

        String ti = frag.getTitle();
        return ti;
    }

}